package com.wgo.precise.client.ui.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Composite;
/**
 *
 * @author petterei
 * @version $Id: EnumPropertySpecification.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public abstract class EnumPropertySpecification<T> extends ClientPropertyDefinition<T> {

	private List<String> enumerationElements = Collections.synchronizedList(new LinkedList<String>());

	private int defaultEnumerationElement = 0;

	public EnumPropertySpecification(Long id, String label,
			PropertyType propertyType) {
		super(id, label, propertyType);
	}

	/**
	 * @return Returns the enumerationValues.
	 */
	public List<String> getEnumerationElements() {
		return enumerationElements;
	}

//	/**
//	 * @see com.wgo.precise.client.ui.model.AbstractPropertySpesification#setDefaultValueFromCellEditor(java.lang.String)
//	 */
//	@Override
//	public void setDefaultValueFromCellEditor(String valueString) {
//		int i = new Integer(valueString);
//		setDefaultEnumerationElement(i);
//	}

	/**
	 * Appends the specified element to the end of this list.
	 *
	 * @param element The element to be appended to this list.
	 */
	public void addEnumerationElement(String element) {
		enumerationElements.add(element);
	}

	/**
	 * Inserts the specified element at the specified position in this list.
	 * Shifts the element currently at that position (if any) and any
	 * subsequent elements to the right (adds one to their indices). If the
	 * specified position is out larger than the size of the list, then the
	 * element is appended at the end of the list.
	 *
	 * @param index index at which the specified element is to be inserted.
	 * @param element element to be inserted.
	 */
	public synchronized void insertEnumerationElement(int index, String element) {
		index = Math.min(index, enumerationElements.size());
		enumerationElements.add(index, element);
		if (index >= defaultEnumerationElement) {
			defaultEnumerationElement++;
		}
	}

	/**
	 * Updates the value of the enumeration label if exists by replaceing the
	 * element at the specified position in this list with the specified
	 * element.
	 *
	 * @param index index of element to replace.
	 * @param element element to be stored at the specified position.
	 * @throws IndexOutOfBoundsException if the specified index is out of
	 *		  range (<tt>index &lt; 0 || index &gt;= size()</tt>).
	 */
	public synchronized void setEnumerationElement(int index, String element) {
		enumerationElements.set(index, element);
	}

	/**
	 * Removes the element at the specified position in this list.  Shifts any
	 * subsequent elements to the left (subtracts one from their indices).
	 * Returns the element that was removed from the list.
	 *
	 * @param index the index of the element to removed.
	 * @return the element previously at the specified position.
	 *
	 * @throws IndexOutOfBoundsException if the specified index is out of
	 * 		  range (<tt>index &lt; 0 || index &gt;= size()</tt>).
	 */
	public String removeEnumerationElement(int index) {
		String returnElement = enumerationElements.remove(index);
		if (defaultEnumerationElement >= index && index != 0) {
			defaultEnumerationElement--;
		}
		return returnElement;
	}

	/**
	 * NOTE! The index starts at zero (0).
	 *
	 * @param defaultEnumerationValue The defaultEnumerationValue to set.
	 */
	public void setDefaultEnumerationElement(int defaultEnumerationValue) {
		this.defaultEnumerationElement = defaultEnumerationValue;
	}

	/**
	 * @see com.wgo.precise.client.ui.model.AbstractPropertySpesification#getCellEditor(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public CellEditor getCellEditor(Composite parent) {
		String[] elements = new String[enumerationElements.size()];
		CellEditor editor = getPropertyType().getCellComboBoxEditor(parent,
				enumerationElements.toArray(elements));
		editor.getControl().setEnabled(!isReadOnly());
		return editor;
	}

	
	public String getTextLabel (int value){
		if (enumerationElements.size() > value && value >= 0 ){
			return enumerationElements.get(value);
		}
		return "Unknown value: " + value;
	}
	
	@Deprecated
	@Override
	public String getDefaultValue() {
		// no need to use..
		return String.valueOf(defaultEnumerationElement);
	}

	@Deprecated
	@Override
	public void setDefaultValue(@SuppressWarnings("unused") String value) {
		//use setDefaultEnumerationElement
	}
}